package cn.itsource.basic.controller;

import cn.itsource.basic.dto.LoginDto;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILogininfoService logininfoService;


    @RequestMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            //业务
            Map<String, Object> map = logininfoService.findByAccount(loginDto);
            return new AjaxResult().setResultObj(map);
        } catch (BusinessException be){
            return new AjaxResult(false,be.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    @GetMapping("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code") String code){
        try {
            return logininfoService.wechatLogin(code);
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试!!!");
        }
    }

    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map){
        try {
            return logininfoService.wechatBinder(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }
}
